﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Jzy.Model.CRM;
using Jzy.Core.Common.Web;
using Jzy.BLL.CRM;
using Frame.Lib.Utilities;


namespace Jzy.Web.CRM.Contact
{
    public partial class Edit : PageEdit
    {
        private CustomerContactBO _contact;
        private CustomerContactBO Contact
        {
            get
            {
                if (_contact == null)
                {
                    _contact = new CustomerContactBO();
                }
                return _contact;

            }
        }

        private string CustomerID
        {
            get
            {
                return Request["CustomerID"];
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                WebUtil.BindListControlWithEnum(Gender, typeof(Gender));
                if (!string.IsNullOrEmpty(PKID))
                {
                    DispInfo();
                }
            }
        }

        private void DispInfo()
        {
            CustomerContactInfo info = this.Contact.GetInfo(this.PKID);

            EntityUtil.SetControl(pnlMain, info);

        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            CustomerContactInfo info = this.Contact.GetInfo(this.PKID);

            EntityUtil.SetEntity(pnlMain, info);

            if (string.IsNullOrEmpty(PKID))
            {
                info.CustomerID = int.Parse(this.CustomerID);
            }

            this.Contact.Save(info);

            if (chkDefault.Checked)
            {
                CustomerBO customer = new CustomerBO();
                CustomerInfo custInfo = customer.GetInfo(this.CustomerID);
                custInfo.ContactName = info.Name;
                customer.Save(custInfo);
            }

            JSUtil.RefreshView();
        }
    }
}
